/* 
 * File:   JBuffers.cpp
 * Author: johnneyshi
 * 
 * Created on 2013年3月4日, 上午10:16
 */

#include "JBuffers.h"
#include "JDefine.h"

JBuffers::JBuffers() {
}


JBuffers::~JBuffers() {
}

JBuffer* JBuffers::GetAvailableBuffer()
{
    for(int i = 0;i < this->pBuffers.size(); ++i)
    {
        if(this->pBuffers[i]->IsInUsed() == false)
        {
            this->pBuffers[i]->SetInUsed();
            return this->pBuffers[i];
        }
    }
    JBuffer* buff = new JBuffer;
    this->pBuffers.push_back(buff);
    return this->pBuffers[this->pBuffers.size() - 1];
}

void JBuffers::Send(int fd, JBuffer* msgBuff)
{
    char buff[BUFF_SIZE];
    int n = 0;
    while((n = msgBuff->Get(buff,BUFF_SIZE)) > 0)
    {
        write(fd,buff,n);
    }
}